﻿using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Linq;

namespace RakutenProxy.AttributeParser
{
    public class FormData
    {
        public List<KeyValuePair<string, string>> Properties { get; set; }
        public string DestinationUrl { get; set; }
        public string StringData { get; set; }

        public FormData()
        {
            Properties = new List<KeyValuePair<string, string>>();
            DestinationUrl = string.Empty;
        }

        public override string ToString()
        {
            var builder = new StringBuilder();
            foreach (var kv in Properties)
            {
                builder.AppendFormat("&{0}={1}", kv.Key, HttpUtility.UrlEncode(kv.Value));
            }

            if (builder.Length > 1) 
                builder.Remove(0, 1);

            return builder.ToString();
        }

        public void Add(string key, string value)
        {
            var kv = new KeyValuePair<string, string>(key, value);
            this.Properties.Add(kv);
        }

        public bool ContainsKey(string key)
        {
            return Properties.Select(l => l.Key).Contains(key);
        }
    }
}
